﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// FoodEdit.xaml 的交互逻辑
    /// </summary>
    public partial class FoodEdit : Window
    {
        public FoodEdit()
        {
            InitializeComponent();
        }
        SQLiteHelper sqlite = new SQLiteHelper();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            getDefault();
            
            setlang();

        }

        private void setlang()
        {
            
            if (lang.Equals("ug"))
            {
                this.FlowDirection = FlowDirection.RightToLeft;
                this.Title = "ئۇچۇر تەھرىرلەش";
                HintAssist.SetHint(txt_name, "ئىسمى");
                HintAssist.SetHint(txt_begin_time, "باشلىنىش ۋاقتى");
                HintAssist.SetHint(txt_end_time, "توختاش ۋاقتى");
                btn_cancel.Content = "قالدۇرۇش";
                btn_ok.Content = "مۇقىملاش";
                 
            }
            else
            {
                this.FlowDirection = FlowDirection.LeftToRight;
                this.Title = "数据编辑";
                HintAssist.SetHint(txt_name, "名称");
                btn_cancel.Content = "取消";
                btn_ok.Content = "确认";
            }
        }

        public string id { get; set; }
        public string lang { get; set; }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string name = btn.Name;

            switch (name)
            {
                case "btn_ok":


                    if (string.IsNullOrEmpty(txt_name.Text)|| string.IsNullOrEmpty(txt_begin_time.Text)|| string.IsNullOrEmpty(txt_end_time.Text))
                    {
                        return;
                    }
                    
                   
                    string item_name = txt_name.Text;

                    string state = "1";

                    string bt =txt_begin_time.Text;

                    string et = txt_end_time.Text;


                    if (radio1_1.IsChecked==true)
                    {
                        state = "1";
                    }else if (radio1_2.IsChecked == true)
                    {
                        state = "2";
                    }else if (radio1_3.IsChecked == true)
                    {
                        state = "3";
                    }

                   // bt=bt.Substring(0, bt.LastIndexOf(":")-1);
                   // et = bt.Substring(0, et.LastIndexOf(":")-1);

                    updateData(id,item_name, state,bt,et);

                    Service service = new Service();
                    //string[] ids = { id };
                   string stt=service.setFoodState(id, state,bt,et);

                   // txt_name.Text = "";
                  //  radio1_1.IsChecked = true;

                    MessageBox.Show(stt);

                    this.Close();

                    break;
                case "btn_cancel":
                    this.Close();

                    break;
            }
        }

        private void getDefault()
        {
            DataRow row=sqlite.getDataRow("select * from food where id='" + id + "'");
            if (row == null)
            {
                return;
            }
            txt_name.Text=row["name"].ToString();
            string st=row["state"].ToString();
            string bt= row["begin_time"].ToString();
            string et = row["end_time"].ToString();
            txt_begin_time.Text = bt;
            txt_end_time.Text = et;
            switch (st)
            {
                case "1":
                    radio1_1.IsChecked = true;
                    break;
                case "2":
                    radio1_2.IsChecked = true;
                    break;
                case "3":
                    radio1_3.IsChecked = true;
                    break;
            }

        }
        private void updateData(string id,string name,string state,string begin_time,string end_time)
        {
            
            
                sqlite.update("update food set state='"+state+"',begin_time='"+begin_time+"',end_time='"+end_time+"' where id='"+id+"'");
            

        }

    }
}
